package com.codeest.geeknews.ui.gank.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeest.geeknews.R;
import com.codeest.geeknews.model.bean.GankItemBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by codeest on 16/8/21.
 */

public class GirlAdapter extends RecyclerView.Adapter<GirlAdapter.ViewHolder> {

    private List<GankItemBean> mList;
    private LayoutInflater inflater;
    private Context mContext;
    private OnItemClickListener onItemClickListener;

    public GirlAdapter(Context mContext, List<GankItemBean> mList) {
        inflater = LayoutInflater.from(mContext);
        this.mList = mList;
        this.mContext = mContext;
    }

    //    /**
//     * 在StaggeredGridLayoutManager瀑布流中,当需要依据图片实际相对高度,不断动态设置ImageView的LayoutParams时,
//     * 会导致快速滑动状态下产生重新排列,重写getItemViewType并设置StaggeredGridLayoutManager.GAP_HANDLING_NONE解决了这个问题，原因目前未知
//     * https://github.com/oxoooo/mr-mantou-android/blob/master/app/src/main/java/ooo/oxo/mr/MainAdapter.java
//     *
//     * @param position
//     * @return
//     */
//    @Override
//    public int getItemViewType(int position) {
//        return Math.round((float) App.SCREEN_WIDTH / (float) mList.get(position).getHeight() * 10f);
//    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_girl, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //存在记录的高度时先Layout再异步加载图片
//        if (mList.get(holder.getAdapterPosition()).getHeight() > 0) {
//            ViewGroup.LayoutParams layoutParams = holder.ivGirl.getLayoutParams();
//            layoutParams.height = mList.get(holder.getAdapterPosition()).getHeight();
//        }
//        Glide.with(mContext).load(mList.get(position).getUrl()).asBitmap()/*.override(200, 200)*/.diskCacheStrategy(DiskCacheStrategy.ALL)
//                .into(new SimpleTarget<Bitmap>(App.SCREEN_WIDTH / 2, App.SCREEN_WIDTH / 2) {
//                    @Override
//                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                        if(holder.getAdapterPosition() != RecyclerView.NO_POSITION) {
//                            if (mList.get(holder.getAdapterPosition()).getHeight() <= 0) {
//                                int width = resource.getWidth();
//                                int height = resource.getHeight();
//                                int realHeight = (App.SCREEN_WIDTH / 2) * height / width;
//                                mList.get(holder.getAdapterPosition()).setHeight(realHeight);
//                                ViewGroup.LayoutParams lp = holder.ivGirl.getLayoutParams();
//                                lp.height = realHeight;
//                            }
//                            holder.ivGirl.setImageBitmap(resource);
//                        }
//                    }
//                });

//        holder.ivGirl.post(new Runnable() {
//            @Override
//            public void run() {
//                Glide.with(mContext)
//                        .load(mList.get(position).getUrl())
//                        .asBitmap()
//                        //.animate(new AlphaAnimation(0f,1f))
//                        //.transform()
//                        .placeholder(R.drawable.ic_profile)
//                        .override(holder.ivGirl.getWidth() / 2, holder.ivGirl.getHeight() / 2)
//                        .diskCacheStrategy(DiskCacheStrategy.ALL)
//                        .into(new SimpleTarget<Bitmap>() {
//                            @Override
//                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                                holder.ivGirl.setImageBitmap(resource);
//                            }
//                        });
//            }
//        });
        Glide.with(mContext)
                .load(mList.get(position).getUrl())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.ivGirl);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    View shareView = view.findViewById(R.id.iv_girl);
                    onItemClickListener.onItemClickListener(holder.getAdapterPosition(), shareView);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_girl)
        ImageView ivGirl;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position, View view);
    }
}
